package com.pomelo.kill.rabbit;

import com.pomelo.kill.model.Order;
import com.pomelo.kill.rabbit.domain.MessageEvent;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Component
public class Subscriber {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 队列中有订单信息
     *
     * @param routingKey 队列名称
     * @param message    信息
     * @param order      订单实体
     */
    public void send(String routingKey, String message, Order order) {
        if (!Objects.isNull(order)) {
            MessageEvent event = new MessageEvent();
            event.setMessage(message);
            event.setData(order);
            rabbitTemplate.convertAndSend(routingKey, event);
        }
    }
}
